package com.yanqu.road.server.cmd.activity.summergames.fromcross;

import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.summergames.LogSummerGamesExchange;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = Protocol.S_SUMMER_GAMES_EXCHANGE_FROM_CROSS, desc = "交换(跨服返回)")
public class SummerGamesExchangeAgreeCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SummerGamesProto.CrossSummerGamesExchangeAgreeRespMsg respMsg = SummerGamesProto.CrossSummerGamesExchangeAgreeRespMsg.parseFrom(packet.getBytes());

        Property cost = new Property();
        cost.addProperty(respMsg.getMedalsId(), BigInteger.ONE);
        player.getModule(PreReduceModule.class).restorePreReduce(cost);

        if(respMsg.getRet() != 0){
            player.sendPacket(Protocol.U_SUMMER_GAMES_EXCHANGE, SummerGamesProto.SummerGamesExchangeAgreeRespMsg.newBuilder().setRet(respMsg.getRet()));
            return;
        }

        player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.SummerGamesActivity, eLogMoneyType.SummerGamesActivityExchangeConsume);

        SummerGamesUserData userData = player.getModule(SummerGamesModule.class).getUserData();

        //日志
        SummerGamesProto.SummerGamesExchangeInfoTemp info = respMsg.getInfo();
        AutoLogMgr.add(new LogSummerGamesExchange(
                userData.getActivityId(),
                userData.getUserId(),
                info.getId(),
                2,
                info.getPlayerId(),  //发起人，对方
                info.getNeedId(),    //我消耗的奖牌
                info.getProvideId(),  //发起人提供的奖牌
                info.getType()
        ));

        //获得
        Property reward = new Property();
        reward.addProperty(info.getProvideId(), BigInteger.ONE);
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.SummerGamesActivity, eLogMoneyType.SummerGamesActivityExchangeReward);

        SummerGamesProto.SummerGamesExchangeAgreeRespMsg.Builder clientMsg = SummerGamesProto.SummerGamesExchangeAgreeRespMsg.newBuilder().setRet(respMsg.getRet());
        clientMsg.setMedalsId(info.getProvideId());
        player.sendPacket(Protocol.U_SUMMER_GAMES_EXCHANGE, clientMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
